package com.medical.controller;

import com.github.pagehelper.PageInfo;
import com.medical.entity.SysYljg;
import com.medical.service.YljgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/yljg")
public class YljgController extends BaseControllelr<SysYljg> {
    @Autowired
    private YljgService yljgService;

    /**
     * 获取用户信息列表
     *
     * @param model
     * @return
     */
    @RequestMapping("/getYljgList")
    public String getYljgList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
        String yljgName = request.getParameter("context");
        currentPage = currentPage != null ? currentPage : 1;
        limit = limit != null ? limit : 10;

        PageInfo<SysYljg> pageInfo = yljgService.getYljgList(yljgName, currentPage, limit);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("context", yljgName);

        return "/yljg/yljg_list";
    }

    /**
     * 打开添加页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/openAdd")
    public String openAdd(Model model) {
        return "/yljg/yljg_add";
    }

    /**
     * 添加用户信息
     *
     * @param sysYljg
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String add(SysYljg sysYljg, HttpServletRequest request, Model model) {
        yljgService.addYljg(sysYljg);
        return getYljgList(1, 10, request, model);
    }

    /**
     * 打开编辑页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/openEdit")
    public String openEdit(Integer id, Model model) {
        SysYljg sysYljg = yljgService.getYljgById(id);
        model.addAttribute("yljg", sysYljg);
        return "/yljg/yljg_edit";
    }

    /**
     * 更新
     *
     * @param sysYljg
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(SysYljg sysYljg, HttpServletRequest request, Model model) {
        yljgService.edit(sysYljg);
        return getYljgList(1, 10, request, model);
    }

    /**
     * 删除
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/delete")
    public String delete(Integer id, HttpServletRequest request, Model model) {
        yljgService.delete(id);
        return getYljgList(1, 10, request, model);
    }
}

